﻿using ProtoImaginer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


namespace ProtoImaginer.ProtoProject
{
    public partial class EditProject : PhoneApplicationPage
    {
        public EditProject()
        {
            InitializeComponent();
        }
        protected string id;
        protected Models.Project projectToEdit = new Project();
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            String parameterString = NavigationContext.QueryString["projectIdToSend"];
            id = parameterString;
            projectToEdit = MainPage.QueryProjectRecordById(id);
            //Fill the text block with project info
            Name_Text.Text = projectToEdit.ProjectName;
            Description_Text.Text = projectToEdit.ProjectDescription;
            Tags_Text.Text = projectToEdit.ProjectTag;
        }

        protected void Complete_Button_Click(object sender, RoutedEventArgs e)
        {
            if (projectToEdit.ProjectName != Name_Text.Text) 
                projectToEdit.ProjectName = Name_Text.Text;
            if (projectToEdit.ProjectDescription != Description_Text.Text) 
                projectToEdit.ProjectDescription = Description_Text.Text;
            if (projectToEdit.ProjectTag != Tags_Text.Text)
                projectToEdit.ProjectTag = Tags_Text.Text;
            projectToEdit.ProjectLastModifiedDate = DateTime.Now;
            MainPage.UpdateRecords(projectToEdit);
            NavigationService.GoBack();
        }

        private void Description_TextChanged(object sender, TextChangedEventArgs e) { }
        private void Tags_TextChanged(object sender, TextChangedEventArgs e) { }
        private void Name_Text_TextChanged(object sender, TextChangedEventArgs e) { }
        private void Name_Text_Focus(object sender, RoutedEventArgs e) { Name_Text.SelectAll(); }
        private void Description_Text_Focus(object sender, RoutedEventArgs e) { Description_Text.SelectAll(); }
        private void Tags_Text_Focus(object sender, RoutedEventArgs e) { Tags_Text.SelectAll(); }

        private void AddPages_Button_Click(object sender, RoutedEventArgs e)
        {
            Guid guid;
            // Create and display the value of two GUIDs.
            guid = Guid.NewGuid();
            Project temp = new Project(
                guid.ToString(),
                1, 
                Name_Text.Text,
                Description_Text.Text,
                Tags_Text.Text
            );
            MainPage.MyProjects.Add(temp);
            //NavigationService.Navigate(new Uri("/ProtoProject/AddPage.xaml", UriKind.Relative));
        }

        

        
    }
}